<%--
  Created by IntelliJ IDEA.
  User: Alexander
  Date: 2025/6/15
  Time: 18:28
  To change this template use File | Settings | File Templates.
--%>

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    request.setCharacterEncoding("UTF-8");
%>
<html>
<head>
    <meta charset="UTF-8">
    <title>社区宠物管理系统 - 注册</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background: url('imgs/2.png') no-repeat center center fixed;
            background-size: cover;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
            position: relative;
        }

        .register-container {
            width: 900px;
            min-height: 500px;
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            display: flex;
            animation: fadeInUp 0.8s ease-out;
        }

        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .left-panel {
            flex: 1;
            background: linear-gradient(135deg, #FFE4E1 0%, #FFF8DC 100%);
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            position: relative;
            overflow: hidden;
        }

        .left-panel::before {
            content: '';
            position: absolute;
            top: -50%;
            left: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255, 182, 193, 0.3) 0%, transparent 70%);
            animation: float 6s ease-in-out infinite;
        }

        @keyframes float {
            0%, 100% { transform: translateY(0px) rotate(0deg); }
            50% { transform: translateY(-20px) rotate(180deg); }
        }

        .pet-image {
            width: 280px;
            height: 280px;
            background-image: url('imgs/10.png');
            background-size: contain;
            background-repeat: no-repeat;
            background-position: center;
            z-index: 2;
            animation: bounce 2s ease-in-out infinite;
        }

        @keyframes bounce {
            0%, 20%, 50%, 80%, 100% {
                transform: translateY(0);
            }
            40% {
                transform: translateY(-10px);
            }
            60% {
                transform: translateY(-5px);
            }
        }

        .pet-title {
            font-size: 2.2rem;
            color: #FF6B6B;
            font-weight: bold;
            margin-top: 15px;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
            z-index: 2;
        }

        .pet-subtitle {
            font-size: 1rem;
            color: #8B4513;
            margin-top: 8px;
            z-index: 2;
        }

        .right-panel {
            flex: 1;
            padding: 40px 50px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            background: white;
            overflow-y: auto;
        }

        .register-header {
            text-align: center;
            margin-bottom: 40px;
        }

        .register-header h2 {
            font-size: 1.8rem;
            color: #333;
            margin-bottom: 8px;
            font-weight: 600;
        }

        .register-header p {
            color: #666;
            font-size: 0.9rem;
        }

        .form-group {
            margin-bottom: 25px;
            position: relative;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 500;
            font-size: 0.95rem;
        }

        .form-group i {
            margin-right: 8px;
            color: #FF6B6B;
        }

        .form-group input {
            width: 100%;
            padding: 15px 20px;
            border: 2px solid #E8E8E8;
            border-radius: 10px;
            font-size: 1rem;
            transition: all 0.3s ease;
            background: #F8F9FA;
        }

        .form-group input:focus {
            outline: none;
            border-color: #FF6B6B;
            background: white;
            box-shadow: 0 0 0 3px rgba(255, 107, 107, 0.1);
        }

        .form-group input::placeholder {
            color: #999;
        }

        /* 密码强度指示器 */
        .password-strength {
            margin-top: 8px;
            font-size: 0.85rem;
        }

        .strength-weak {
            color: #D32F2F;
        }

        .strength-medium {
            color: #F57C00;
        }

        .strength-strong {
            color: #388E3C;
        }

        /* 错误输入框样式 */
        .form-group input.error {
            border-color: #D32F2F;
            background: #FFEBEE;
        }

        .form-group input.success {
            border-color: #388E3C;
            background: #E8F5E8;
        }

        /* 实时验证消息 */
        .validation-message {
            margin-top: 5px;
            font-size: 0.8rem;
            display: none;
        }

        .validation-message.error {
            color: #D32F2F;
            display: block;
        }

        .validation-message.success {
            color: #388E3C;
            display: block;
        }

        .register-btn {
            width: 100%;
            padding: 16px;
            background: linear-gradient(135deg, #FF6B6B 0%, #FF8E8E 100%);
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 1.1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 20px;
            box-shadow: 0 4px 15px rgba(255, 107, 107, 0.3);
        }

        .register-btn:hover:not(:disabled) {
            background: linear-gradient(135deg, #FF5252 0%, #FF7575 100%);
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(255, 107, 107, 0.4);
        }

        .register-btn:active {
            transform: translateY(0);
        }

        .register-btn:disabled {
            background: #CCCCCC;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .success-message {
            background: #E8F5E8;
            color: #2E7D32;
            padding: 12px 15px;
            border-radius: 8px;
            margin-top: 20px;
            border-left: 4px solid #4CAF50;
            font-size: 0.9rem;
        }

        .error-message {
            background: #FFE6E6;
            color: #D32F2F;
            padding: 12px 15px;
            border-radius: 8px;
            margin-top: 20px;
            border-left: 4px solid #D32F2F;
            font-size: 0.9rem;
        }

        .success-message i, .error-message i {
            margin-right: 8px;
        }

        .login-link {
            text-align: center;
            margin-top: 30px;
        }

        .login-link p {
            color: #666;
            font-size: 0.9rem;
        }

        .login-link a {
            color: #FF6B6B;
            text-decoration: none;
            font-weight: 500;
            transition: color 0.3s ease;
        }

        .login-link a:hover {
            color: #FF5252;
        }

        .decorative-paws {
            position: absolute;
            font-size: 1.2rem;
            color: rgba(255, 107, 107, 0.2);
            animation: rotate 8s linear infinite;
        }

        .paw-1 {
            top: 15px;
            right: 25px;
            animation-delay: 0s;
        }

        .paw-2 {
            bottom: 25px;
            right: 15px;
            animation-delay: 2s;
        }

        .paw-3 {
            top: 45%;
            right: 8px;
            animation-delay: 4s;
        }

        @keyframes rotate {
            from {
                transform: rotate(0deg);
            }
            to {
                transform: rotate(360deg);
            }
        }

        @media (max-width: 768px) {
            .register-container {
                width: 95%;
                flex-direction: column;
            }

            .left-panel {
                height: 300px;
            }

            .pet-image {
                width: 180px;
                height: 180px;
            }

            .pet-title {
                font-size: 1.6rem;
            }

            .right-panel {
                padding: 30px 25px;
            }
        }
    </style>
</head>
<body>
<div class="register-container">
    <!-- 左侧面板 -->
    <div class="left-panel">
        <div class="pet-image"></div>
        <h1 class="pet-title">宠物领养</h1>
        <p class="pet-subtitle">给每个小生命一个温暖的家</p>

        <!-- 装饰性爪印 -->
        <i class="fas fa-paw decorative-paws paw-1"></i>
        <i class="fas fa-paw decorative-paws paw-2"></i>
        <i class="fas fa-paw decorative-paws paw-3"></i>
    </div>

    <!-- 右侧注册表单 -->
    <div class="right-panel">
        <div class="register-header">
            <h2>用户注册</h2>
            <p>加入我们的宠物社区大家庭</p>
        </div>

        <form id="registerForm" method="post" action="${pageContext.request.contextPath}/register">
            <div class="form-group">
                <label><i class="fas fa-user"></i> 用户名</label>
                <input type="text" id="username" name="username" required
                       placeholder="请输入用户名" maxlength="20">
                <div id="usernameMessage" class="validation-message"></div>
            </div>

            <div class="form-group">
                <label><i class="fas fa-lock"></i> 密码</label>
                <input type="password" id="password" name="password" required
                       placeholder="6-20位，必须包含大小写字母和数字"
                       minlength="6" maxlength="20">
                <div id="passwordMessage" class="validation-message"></div>
                <div id="passwordStrength" class="password-strength"></div>
            </div>

            <div class="form-group">
                <label><i class="fas fa-lock"></i> 确认密码</label>
                <input type="password" id="confirmPassword" name="confirmPassword" required
                       placeholder="请再次输入密码" minlength="6" maxlength="20">
                <div id="confirmPasswordMessage" class="validation-message"></div>
            </div>

            <button type="submit" id="submitBtn" class="register-btn">
                <i class="fas fa-user-plus"></i> 立即注册
            </button>
        </form>

        <!-- 错误消息 -->
        <c:if test="${not empty sessionScope.error}">
            <div class="error-message">
                <i class="fas fa-exclamation-triangle"></i> ${sessionScope.error}
            </div>
            <%-- 显示一次错误后立即清除 --%>
            <%
                session.removeAttribute("error");
            %>
        </c:if>

        <!-- 成功消息 -->
        <c:if test="${not empty sessionScope.success}">
            <div class="success-message">
                <i class="fas fa-check-circle"></i> ${sessionScope.success}
            </div>
            <%-- 显示一次成功消息后立即清除 --%>
            <%
                session.removeAttribute("success");
            %>
        </c:if>

        <!-- 登录链接 -->
        <div class="login-link">
            <p>
                已有账户？
                <a href="${pageContext.request.contextPath}/login.jsp">
                    立即登录
                </a>
            </p>
        </div>
    </div>
</div>

<script>
    // 密码强度验证正则表达式
    const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[A-Za-z\d]{6,20}$/;

    // 获取表单元素
    const form = document.getElementById('registerForm');
    const usernameInput = document.getElementById('username');
    const passwordInput = document.getElementById('password');
    const confirmPasswordInput = document.getElementById('confirmPassword');
    const submitBtn = document.getElementById('submitBtn');

    // 获取消息显示元素
    const usernameMessage = document.getElementById('usernameMessage');
    const passwordMessage = document.getElementById('passwordMessage');
    const confirmPasswordMessage = document.getElementById('confirmPasswordMessage');
    const passwordStrength = document.getElementById('passwordStrength');

    // 验证状态
    let validationState = {
        username: false,
        password: false,
        confirmPassword: false
    };

    // 用户名验证
    usernameInput.addEventListener('input', function() {
        const username = this.value.trim();

        if (username.length === 0) {
            showValidationMessage(usernameMessage, '', '', usernameInput);
            validationState.username = false;
        } else if (username.length > 20) {
            showValidationMessage(usernameMessage, '用户名长度不能超过20个字符', 'error', usernameInput);
            validationState.username = false;
        } else if (username.length < 2) {
            showValidationMessage(usernameMessage, '用户名至少需要2个字符', 'error', usernameInput);
            validationState.username = false;
        } else {
            showValidationMessage(usernameMessage, '用户名格式正确', 'success', usernameInput);
            validationState.username = true;
        }

        updateSubmitButton();
    });

    // 密码验证
    passwordInput.addEventListener('input', function() {
        const password = this.value;

        if (password.length === 0) {
            showValidationMessage(passwordMessage, '', '', passwordInput);
            passwordStrength.textContent = '';
            validationState.password = false;
        } else if (!passwordRegex.test(password)) {
            let errorMsg = '密码不符合要求：';
            if (password.length < 6) {
                errorMsg += '至少需要6位字符';
            } else if (password.length > 20) {
                errorMsg += '不能超过20位字符';
            } else if (!/(?=.*[a-z])/.test(password)) {
                errorMsg += '必须包含小写字母';
            } else if (!/(?=.*[A-Z])/.test(password)) {
                errorMsg += '必须包含大写字母';
            } else if (!/(?=.*\d)/.test(password)) {
                errorMsg += '必须包含数字';
            }

            showValidationMessage(passwordMessage, errorMsg, 'error', passwordInput);
            updatePasswordStrength(password);
            validationState.password = false;
        } else {
            showValidationMessage(passwordMessage, '密码格式正确', 'success', passwordInput);
            updatePasswordStrength(password);
            validationState.password = true;
        }

        // 如果确认密码已输入，重新验证确认密码
        if (confirmPasswordInput.value) {
            validateConfirmPassword();
        }

        updateSubmitButton();
    });

    // 确认密码验证
    confirmPasswordInput.addEventListener('input', validateConfirmPassword);

    function validateConfirmPassword() {
        const password = passwordInput.value;
        const confirmPassword = confirmPasswordInput.value;

        if (confirmPassword.length === 0) {
            showValidationMessage(confirmPasswordMessage, '', '', confirmPasswordInput);
            validationState.confirmPassword = false;
        } else if (password !== confirmPassword) {
            showValidationMessage(confirmPasswordMessage, '两次输入的密码不一致', 'error', confirmPasswordInput);
            validationState.confirmPassword = false;
        } else {
            showValidationMessage(confirmPasswordMessage, '密码确认正确', 'success', confirmPasswordInput);
            validationState.confirmPassword = true;
        }

        updateSubmitButton();
    }

    // 显示验证消息
    function showValidationMessage(element, message, type, inputElement) {
        element.textContent = message;
        element.className = 'validation-message ' + type;

        // 更新输入框样式
        inputElement.className = inputElement.className.replace(/\s?(error|success)/g, '');
        if (type) {
            inputElement.classList.add(type);
        }
    }

    // 更新密码强度显示
    function updatePasswordStrength(password) {
        let strength = 0;
        let strengthText = '';

        if (password.length >= 6) strength++;
        if (/[a-z]/.test(password)) strength++;
        if (/[A-Z]/.test(password)) strength++;
        if (/\d/.test(password)) strength++;
        if (password.length >= 10) strength++;

        if (strength <= 2) {
            strengthText = '密码强度：弱';
            passwordStrength.className = 'password-strength strength-weak';
        } else if (strength <= 3) {
            strengthText = '密码强度：中等';
            passwordStrength.className = 'password-strength strength-medium';
        } else {
            strengthText = '密码强度：强';
            passwordStrength.className = 'password-strength strength-strong';
        }

        passwordStrength.textContent = strengthText;
    }

    // 更新提交按钮状态
    function updateSubmitButton() {
        const allValid = validationState.username &&
            validationState.password &&
            validationState.confirmPassword;

        submitBtn.disabled = !allValid;
    }

    // 表单提交验证
    form.addEventListener('submit', function(e) {
        // 最终验证
        const username = usernameInput.value.trim();
        const password = passwordInput.value;
        const confirmPassword = confirmPasswordInput.value;

        if (!username || username.length > 20) {
            e.preventDefault();
            alert('请输入有效的用户名（不超过20个字符）');
            return;
        }

        if (!passwordRegex.test(password)) {
            e.preventDefault();
            alert('密码格式不正确：需6-20位，必须同时包含大小写字母和数字');
            return;
        }

        if (password !== confirmPassword) {
            e.preventDefault();
            alert('两次输入的密码不一致');
            return;
        }
    });

    // 初始化按钮状态
    updateSubmitButton();
</script>
</body>
</html>